package com.rr.program._bigDecimal;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 测试进度、使用到的BigDecimal类型测试
 *
 * @author : RENBIAO
 * @createTime : 2021/6/23 16:20
 */
public class ProgressBigDecimalTest {

    public static void main(String[] args) {

        //这三个字段可以是null的时候，测试通过
        Long soldInventory = 100L;
        Long lockInventory = 30L;
        Long displayInventory = 200L;

        BigDecimal realRateProgress = BigDecimal.ZERO;
        if (null != soldInventory && soldInventory > 0 || (null != lockInventory && lockInventory > 0)) {

            BigDecimal newSoldInventory = null == soldInventory ? BigDecimal.ZERO : new BigDecimal(soldInventory);
            BigDecimal newLockInventory = null == lockInventory ? BigDecimal.ZERO : new BigDecimal(lockInventory);
            BigDecimal newDisplayInventory = null == displayInventory ? BigDecimal.ZERO : new BigDecimal(displayInventory);

            if ((newDisplayInventory.add(newSoldInventory)).compareTo(BigDecimal.ZERO) > 0) {
                realRateProgress = (newSoldInventory.add(newLockInventory)).divide(newDisplayInventory.add(newSoldInventory), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
                System.out.println("计算后的进度：" + realRateProgress.setScale(0, RoundingMode.HALF_UP));
            }
        }
        //进度比较
        BigDecimal virtualRateProgress = BigDecimal.valueOf(30);
        if (realRateProgress.compareTo(virtualRateProgress) > 0) {
            System.out.println("最后的进度是：" + realRateProgress.setScale(0, RoundingMode.HALF_UP));
        } else {
            System.out.println("最后的进度是：" + virtualRateProgress);
        }
    }
}
